package com.study.compose.beta.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.unit.dp
import com.module.compose.base.composable.imageLoader.rememberCoilPainter
import com.module.compose.base.composable.pager.*
import com.study.compose.beta.utils.rememberRandomSampleImageUrl
import kotlin.math.absoluteValue

/**
 * 作者：zhangjunfei 2021/6/18 2:18 下午
 * 主页：https://gitee.com/ZhangQQ_123/projects
 * 描述：PagerView
 */

@OptIn(ExperimentalPagerApi::class)
@Composable
fun PagerSample() {
    val pagerState = rememberPagerState(pageCount = 10, initialOffscreenLimit = 2)

    Column(Modifier.fillMaxWidth()) {
        HorizontalPager(
            state = pagerState,
            modifier = Modifier.fillMaxWidth()
        ) { page ->
            Image(
                painter = rememberCoilPainter(
                    request = rememberRandomSampleImageUrl(width = 600, height = 300),
                    fadeIn = true,
                ),
                contentDescription = null,
                modifier = Modifier
                    //添加过渡动画效果
                    .graphicsLayer {
                        // Calculate the absolute offset for the current page from the
                        // scroll position. We use the absolute value which allows us to mirror
                        // any effects for both directions
                        val pageOffset = calculateCurrentOffsetForPage(page).absoluteValue

                        // We animate the scaleX + scaleY, between 85% and 100%
                        lerp(
                            start = 0.85f,
                            stop = 1f,
                            fraction = 1f - pageOffset.coerceIn(0f, 1f)
                        ).also { scale ->
                            scaleX = scale
                            scaleY = scale
                        }

                        // We animate the alpha, between 50% and 100%
                        alpha = lerp(
                            start = 0.5f,
                            stop = 1f,
                            fraction = 1f - pageOffset.coerceIn(0f, 1f)
                        )
                    }
                    .fillMaxWidth(0.8f)
                    .aspectRatio(2f)
                    .clip(shape = RoundedCornerShape(5.dp))
            )
        }

        //指示器
        HorizontalPagerIndicator(
            pagerState = pagerState,
            modifier = Modifier
                .align(Alignment.CenterHorizontally)
                .padding(16.dp),
        )
    }

}